package com.briup.estore.controller;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.briup.estore.bean.Address;
import com.briup.estore.bean.Book;
import com.briup.estore.bean.Category;
import com.briup.estore.bean.Customer;
import com.briup.estore.bean.OrderForm;
import com.briup.estore.bean.OrderLine;
import com.briup.estore.bean.ShopCar;
import com.briup.estore.bean.ShopCarItem;
import com.briup.estore.service.IAddressService;
import com.briup.estore.service.IBookService;
import com.briup.estore.service.ICategoryService;
import com.briup.estore.service.IOrderService;
import com.briup.estore.service.IShopCarService;

@Controller
public class to_jsp {

	@Autowired
	private ICategoryService categoryService;

	@Autowired
	private IBookService bookService;

	@Autowired
	private IShopCarService shopCarService;

	@Autowired
	private IAddressService addressService;

	@Autowired
	private IOrderService orderService;

	@RequestMapping("/index")
	public ModelAndView to_index(ModelAndView model) {
		// 查询一级栏目和二级栏目
		// 查询第一个一级栏目的书籍
		List<Category> categoryWithBooks = categoryService.findAllCategoryWithBooks();

		model.addObject("categoryWithBooks", categoryWithBooks);
		model.setViewName("/index");
		return model;
	}

	@RequestMapping("/login")
	public String to_login(HttpServletRequest request) {
		return "/login";
	}

	@RequestMapping("/register")
	public String to_register() {
		return "/register";
	}

	@RequestMapping("/viewBook")
	public ModelAndView to_viewBook(BigInteger id, ModelAndView model) {
		// 书籍信息
		// 所有栏目
		// 书籍所在的一级栏目
		List<Category> allCategories = categoryService.findAllCategories();
		Book book = bookService.findBookById(id);
		model.addObject("allCategories", allCategories);
		model.addObject("book", book);
		model.setViewName("/viewBook");

//		System.out.println("book>>" + book);
		return model;
	}

	@RequestMapping("/list")
	public ModelAndView to_list(BigInteger id, ModelAndView model) {

		// 该栏目对应的一级栏目
		Category categoryWithCategory = categoryService.findCategoryWithCategoryById(id);
		// 该栏目包含的书籍信息
		Category categoryWithBooks = categoryService.findCategoryWithBooksById(id);
		// 全部一级栏目
		List<Category> allCategories = categoryService.findAllCategories();
		// 全部出版社
		List<String> publisherList = bookService.findAllPublisher();
		model.addObject("allCategories", allCategories);
		model.addObject("categoryWithCategory", categoryWithCategory);
		model.addObject("categoryWithBooks", categoryWithBooks);
		model.addObject("publisherList", publisherList);

		model.setViewName("/list");
		return model;
	}

	@RequestMapping("/test")
	public void test() {

		categoryService.test();
	}

	@RequestMapping("login/shopCart")
	public ModelAndView to_shopCart(HttpSession session, ModelAndView model) {
		Customer customer = (Customer) session.getAttribute("customer");
		ShopCar shopCar = shopCarService.findShopCarWithShipCarItemByCustomerId(customer.getId());
		model.addObject("shopCar", shopCar);
		model.setViewName("/shopCart");

//		System.out.println("handler-shopCar>>>" + shopCar);
		return model;
	}

	@RequestMapping("/login/confirm")
	public ModelAndView to_confirm(ModelAndView model, HttpServletRequest request, HttpSession session) {
		String[] values = request.getParameterValues("selectname");
		List<BigInteger> select = new ArrayList<BigInteger>();
		// 查询地址
		Customer customer = (Customer) session.getAttribute("customer");
		List<Address> addresses = addressService.findAddresses(customer.getId());
		if (addresses == null) {
			addresses = new ArrayList<Address>();
		}
		model.addObject("addresses", addresses);

//		System.out.println(addresses);

		if (values != null && values.length != 0) {
			for (String s : values) {
				select.add(new BigInteger(s));
			}
		}
		// 查找订单项
		List<ShopCarItem> shopCarItems = shopCarService.findShopCarItemByIds(select);
		double total = 0.0;
		for (ShopCarItem shopCarItem : shopCarItems) {
			total += shopCarItem.getCost();
		}
		model.addObject("total", total);
		model.addObject("shopCarItems", shopCarItems);
		model.addObject("length", shopCarItems.size());
		model.setViewName("/confirm");
		return model;
	}

	@RequestMapping("/login/orderList")
	public ModelAndView to_order(HttpSession session, ModelAndView model) {
		Customer customer = (Customer) session.getAttribute("customer");
		List<OrderForm> orders = new ArrayList<OrderForm>();
		List<OrderForm> list = orderService.findOrderFormsByCustomerId(customer.getId());
		orders = orderService.findOrderFormsByCustomerId(customer.getId());
		model.addObject("orders", orders);
		model.setViewName("/orderList");
		return model;
	}

	@RequestMapping("/login/orderdetail")
	public ModelAndView to_orderdetail(ModelAndView model, BigInteger id) {
		List<OrderLine> lines = orderService.findOrderLinesByOrderId(id);
		double total = 0.0;
		for (OrderLine orderLine : lines) {
			total += orderLine.getCost();
		}
		model.addObject("total", total);
		model.addObject("lines", lines);
		model.setViewName("/orderdetail");
		return model;
	}

	@RequestMapping("/error/404")
	public String to_404(HttpServletRequest request) {
		return "/404";
	}

	@RequestMapping("/error/400")
	public String to_400(HttpServletRequest request) {
		return "/400";
	}

	@RequestMapping("/error/500")
	public String to_500(HttpServletRequest request) {
		return "/400";
	}

}
